package cn.hrlly.tao2shou.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.adapter.SellerApplicationAdapter;
import cn.hrlly.tao2shou.domain.ApplicationList;
import cn.hrlly.tao2shou.domain.Applications;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.ApplicationListPresenter;
import cn.hrlly.tao2shou.presenter.ApplicationListPresenterImpl;
import cn.hrlly.tao2shou.presenter.CancelApplicationPresenter;
import cn.hrlly.tao2shou.presenter.CancelApplicationPresenterImpl;
import cn.hrlly.tao2shou.presenter.ConfirmApplicationPresenter;
import cn.hrlly.tao2shou.presenter.ConfirmApplicationPresenterImpl;
import cn.hrlly.tao2shou.utils.NetUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.ApplicationListView;
import cn.hrlly.tao2shou.view.view_interface.CancelApplicationView;
import cn.hrlly.tao2shou.view.view_interface.ConfirmApplicationView;

public class SellerApplicationActivity extends BaseActivity implements ApplicationListView, CancelApplicationView, ConfirmApplicationView {

    @Bind(R.id.lvApplication)
    ListView lvApplication;
    private ArrayList<Applications> list;
    private ApplicationListPresenter applicationListPresenter;
    private CancelApplicationPresenter cancelApplicationPresenter;
    private ConfirmApplicationPresenter confirmApplicationPresenter;
    public String applicationId;
    private SellerApplicationAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seller);
        //绑定控件
        ButterKnife.bind(this);
        //初始化
        init();
        //点击事件
        clickListview();
    }

    //初始化
    public void init(){
        list=new ArrayList<Applications>();
        applicationId=null;
        applicationListPresenter=new ApplicationListPresenterImpl(this,this);
        cancelApplicationPresenter=new CancelApplicationPresenterImpl(this);
        confirmApplicationPresenter=new ConfirmApplicationPresenterImpl(this);
        if(NetUtil.ping(this)){
            applicationListPresenter.showApplicationList();
        }else {
            applicationListPresenter.showCacheApplicationList();
        }
    }

    //点击事件
    public void clickListview(){
        lvApplication.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(SellerApplicationActivity.this,ApplicationDetailsActivity.class);
                intent.putExtra(Config.key_application_id,list.get(position).getApplicationId());
                startActivity(intent);
            }
        });
    }

    //以下是cancelApplicationView的操作
    @Override
    public String getApplicationId() {
        return applicationId;
    }

    @Override
    public void finishActivity() {
        finish();
    }

    //以下是applicationListView的操作
    @Override
    public void showListView(ApplicationList applicationList) {
        list.addAll(applicationList.getApplications());
        adapter=new SellerApplicationAdapter(this,list,cancelApplicationPresenter,confirmApplicationPresenter,this);
        lvApplication.setAdapter(adapter);
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public int getApplicationType() {
        return 1;
    }
}
